package cr.novatec.test.server;

import java.util.ArrayList;
import java.util.List;

import cr.novatec.test.client.Animal;
import cr.novatec.test.client.PersistenceService;

import com.google.appengine.api.datastore.GeoPt;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

@SuppressWarnings("serial")
public class PersistenceServiceImpl extends RemoteServiceServlet implements
PersistenceService {
    
    //Method must be present in Service Interface and Async Interface
    public void persistAnimal(String color, String species) {
        
        //The Objectify service for the entity must be registered before any 
        //operations can be executed
        ObjectifyService.register(Animal.class);
        Objectify ofy = ObjectifyService.begin();
        
        Animal animal = new Animal();
        //Use setters to populate the object
        //the Id will be auto generated and does not need to be set
        animal.setColor(color);
        animal.setSpecies(species);
        
        ofy.put(animal);
        
    }

	public Animal getAnimal() throws IllegalArgumentException {
		Animal a = new Animal();
		a.setColor("BLACK");
		a.setSpecies("especie" );		
		a.setPoint(new GeoPt(39.509F, -98.434F));
		return a;
	}

	@Override
	public List<Animal> getAnimals() throws IllegalArgumentException {
		List<Animal> list = new ArrayList<Animal>();
		Animal a = new Animal();
		a.setColor("BLACK");
		a.setSpecies("especie" );		
		a.setPoint(new GeoPt(39.509F, -98.434F));
		
		list.add(a);

		Animal b = new Animal();
		b.setColor("WHITE");
		b.setSpecies("WWespecie" );		
		b.setPoint(new GeoPt(39.509F, -98.434F));
		
		list.add(b);
				
		return list;
	}

}


